package edu.ccit.shudong.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import edu.ccit.shudong.dto.PostDTO;
import edu.ccit.shudong.entity.Message;
import edu.ccit.shudong.entity.Post;
import edu.ccit.shudong.mapper.MessageMapper;
import edu.ccit.shudong.mapper.PostMapper;
import edu.ccit.shudong.service.IMessageService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import edu.ccit.shudong.utils.JwtUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

import static net.sf.jsqlparser.util.validation.metadata.NamedObject.user;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Suting Luo
 * @since 2024-10-09
 */
@Service
public class MessageServiceImpl extends ServiceImpl<MessageMapper, Message> implements IMessageService {
    @Resource
    private MessageMapper messageMapper;
    @Resource
    private PostMapper postMapper;

    @Override
    public void addMessage(Message message) {

    }

    @Override
    public List<Message> findListByUserId(long userId) {
        return this.list(new QueryWrapper<Message>().eq("user_id", userId));
    }

    @Override
    public List<PostDTO> findPostMessagesByUserId(long userId) {
        // 读取归属于该用户的消息
        List<Message> msgs = findListByUserId(userId);

        // 创建帖子列表
        List<PostDTO> posts = new ArrayList<>();

        // 创建一个Set，用于PostID去重
        Set<Long> postIds = new HashSet<>();

        // 遍历所有消息
        for (Message msg : msgs) {
            postIds.add(msg.getPostId());
        }

        // 遍历postIds这个set
        for (Long postId : postIds) {
            Post post = postMapper.selectById(postId);      // 找到对应的post
            if (post != null) {
                PostDTO postDTO = new PostDTO();                // 创建一个DTO对象
                postDTO.setPost(post);                          // 设置Post

                // 把归属于这个Post的所有Message加到列表中
                for (Message msg : msgs) {
                    if (msg.getPostId() == postId) {
                        postDTO.getMessages().add(msg);
                    }
                }

                posts.add(postDTO);                             // 添加到列表
            }
        }

        return posts;
    }

    @Override
    public PostDTO getProfileByToken(String token) {
        long userId = JwtUtils.getUserId(token);
        List<Message> messages = findListByUserId(userId);
        PostDTO postDTO = new PostDTO();
        postDTO.setUserId(userId);

        // 创建一个Set，用于PostID去重
        Set<Long> postIds = new HashSet<>();

        // 遍历所有消息
        for (Message msg : messages) {
            postIds.add(msg.getPostId());
        }

        // 遍历postIds这个set
        for (Long postId : postIds) {
            Post post = postMapper.selectById(postId);      // 找到对应的post
            if (post != null) {
                postDTO.setPost(post);                          // 设置Post

                // 把归属于这个Post的所有Message加到列表中
                for (Message msg : messages) {
                    if (msg.getPostId() == postId) {
                        postDTO.addMessage(msg);
                    }
                }
            }
        }
        return postDTO;
    }
}
